package com.ruoyi.common.push;
import com.alibaba.fastjson.JSONObject;
import com.getui.push.v2.sdk.ApiHelper;  
import com.getui.push.v2.sdk.GtApiConfiguration;  
import com.getui.push.v2.sdk.api.PushApi;  
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;  
import com.getui.push.v2.sdk.dto.req.Settings;  
import com.getui.push.v2.sdk.dto.req.message.PushChannel;  
import com.getui.push.v2.sdk.dto.req.message.PushDTO;  
import com.getui.push.v2.sdk.dto.req.message.PushMessage;  
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;  
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;  
import com.getui.push.v2.sdk.dto.req.message.android.Ups;  
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;  
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;  
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;

import java.util.HashMap;
import java.util.Map;  

/**
 * 	unipush 离线推送
 * @author DEREK
 *
 */
public class UnipushUtil {


	/**
	 * 	推送消息
	 * @param title		标题
	 * @param content	内容
	 * @param cid		设备唯一编码
	 * @return
	 */
	public static boolean pushMsg(String title,String content,String cid) {
		GtApiConfiguration apiConfiguration = new GtApiConfiguration();  
		//填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取  
		apiConfiguration.setAppId("");  
		apiConfiguration.setAppKey("");  
		apiConfiguration.setMasterSecret("");  
		apiConfiguration.setDomain("https://restapi.getui.com/v2/");  
		// 实例化ApiHelper对象，用于创建接口对象  
		ApiHelper apiHelper = ApiHelper.build(apiConfiguration);  
		// 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi  
		PushApi pushApi = apiHelper.creatApi(PushApi.class);  
		//根据cid进行单推  
		PushDTO<Audience> pushDTO = new PushDTO<Audience>();  
		// 设置推送参数，requestid需要每次变化唯一  
		pushDTO.setRequestId(System.currentTimeMillis() + "");  
		Settings settings = new Settings();  
		pushDTO.setSettings(settings);  
		//消息有效期，走厂商消息必须设置该值  
		settings.setTtl(3600000);  
		//在线走个推通道时推送的消息体  
		PushMessage pushMessage = new PushMessage();  
		pushDTO.setPushMessage(pushMessage);  
		//此格式的透传消息由 unipush 做了特殊处理，会自动展示通知栏。开发者也可自定义其它格式，在客户端自己处理。  
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("title", title);
		jsonObject.put("content", content);
		jsonObject.put("payload", "自定义数据");
		pushMessage.setTransmission(jsonObject.toJSONString()); 
		
		// 设置接收人信息  
		Audience audience = new Audience();  
		pushDTO.setAudience(audience);  
		audience.addCid(cid);  
		//设置离线推送时的消息体  
		PushChannel pushChannel = new PushChannel();  
		//安卓离线厂商通道推送的消息体  
		AndroidDTO androidDTO = new AndroidDTO();  
		Ups ups = new Ups();  
		ThirdNotification thirdNotification = new ThirdNotification();  
		ups.setNotification(thirdNotification);  
		thirdNotification.setTitle(title);  
		thirdNotification.setBody(content);  
		thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);  
		Map<String, Map<String, Object>> options = new HashMap<String, Map<String,Object>>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("/message/android/notification/badge/class", "io.dcloud.PandoraEntry");
		map.put("/message/android/notification/badge/add_num", 1);
		options.put("HW", map);
		ups.setOptions(options);
		ups.setOptions(options);
		//注意：intent参数必须按下方文档（特殊参数说明）要求的固定格式传值，intent错误会导致客户端无法收到消息  
//		thirdNotification.setIntent("https://www.baidu.com");  
//		thirdNotification.setUrl("https://www.baidu.com");
		androidDTO.setUps(ups);  
		pushChannel.setAndroid(androidDTO);  
		//ios离线apn通道推送的消息体  
		Alert alert = new Alert();  
		alert.setTitle(title);  
		alert.setBody(content);  
		Aps aps = new Aps();  
		aps.setContentAvailable(0);  
		aps.setSound("default");  
		aps.setAlert(alert);  
		IosDTO iosDTO = new IosDTO();  
		iosDTO.setAps(aps);  
		iosDTO.setType("notify"); 
		iosDTO.setAutoBadge("+1");
		pushChannel.setIos(iosDTO);  
		pushDTO.setPushChannel(pushChannel);
		// 进行cid单推  
		ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);  
		if (apiResult.isSuccess()) {  
			// success  
			System.out.println(apiResult.getData());  
			return true;
		} else {  
			// failed  
			System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());  
			return false;
		}  
	}


	public static void main(String[] args) {
		pushMsg("解锁微信1", "我要解锁你的微信啦1", "1ad2bf9b6c4b2c73f763cd708aeeb943");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("title", "标题");
		jsonObject.put("content", "内容");
//		System.out.println(jsonObject.toString());
	}
}
